package com.jingan.nursinghome.regulatory.dialog;

import android.graphics.Color;
import android.graphics.Rect;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.adapter.MicrometerTableAdapter3;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.model.Topic;
import com.jingan.nursinghome.regulatory.view.KongView;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.RectificationView;
import com.jingan.nursinghome.regulatory.view.SpannerView;
import com.jingan.nursinghome.regulatory.view.iView;

import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.dialog.AlertDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/11/6.
 */

public class ZhangaiXiangDialog extends BaseDialog {

    private static ZhangaiXiangDialog hostoryDialog;

    static int mode = 0;//0千分表，1日常表，2专项表。

    public static BaseListenner mCallback;

    //参数三用于回调。适配器更新。
    public static ZhangaiXiangDialog getInstance(BaseActivity baseActivity, List<Topic> Topics, int m, BaseListenner callback) {
        myTopics = Topics;
        mode = m;
        mCallback = callback;
        if (isFinishing(hostoryDialog)) {
            hostoryDialog = new ZhangaiXiangDialog(baseActivity);
        }
        //Log.e("test","整改数量:\t"+Topics.size());
        return hostoryDialog;
    }

    public ZhangaiXiangDialog(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_history, false, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right);
        initUI();
        adapterUI();
    }

    MyActionBar myActionBar;

    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;

    private void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "整改项");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                dismiss();
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(baseActivity) {
            @Override
            public boolean canScrollVertically() {
                //return canScrollVertically;
                return true;
            }
        };
        recyclerView.setLayoutManager(linearLayoutManager);
        //recyclerView.addItemDecoration(new MySpacesItemDecoration(baseActivity, 0));
    }

    private void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(baseActivity, findViewById(R.id.dialog_micrometertableos_constraintLayout));
    }

    private static List<Topic> myTopics;

    @Override
    protected void listener() {
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
                super.getItemOffsets(outRect, itemPosition, parent);
                //设置RecyClerView子item项左右边距
//                outRect.left = 10;
//                outRect.right = 10;
//
//                if (myTopics.get(itemPosition).getIndex() == null) {
//                    return;
//                }
//                if (myTopics.get(itemPosition).getIndex().trim().equals("0")) {
//                    outRect.top = ProportionUtils.getInstance().adapterInt(20);
//                } else {
//                    outRect.top = ProportionUtils.getInstance().adapterInt(0);
//                }
            }
        });
        if (mode == 0 || mode == 1) {
            //千分表，日常表
            recyclerView.setAdapter(new MicrometerTableAdapter(baseActivity));
        }
        if (mode == 2) {
            //专项表
            recyclerView.setAdapter(new MicrometerTableAdapter3(baseActivity));
        }

    }

    @Override
    protected void recycleView() {
        if (mCallback != null) {
            mCallback.onResult(null);
        }
    }

    //适配器开始

    public class MicrometerTableAdapter extends RecyclerView.Adapter<ZhangaiXiangDialog.MicrometerTableAdapter.MyViewHolder> {
        BaseActivity baseActivity;

        public MicrometerTableAdapter(BaseActivity baseActivity) {
            this.baseActivity = baseActivity;
        }

        //ViewHolder和View绑定在一起，构造函数一般都是初始化控件
        public class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView txt_title;
            iView txt_question;
            com.jingan.nursinghome.regulatory.view.triangleView triangleView;
            SpannerView spanerScore;
            RectificationView rectifi;
            View ling;

            public MyViewHolder(View itemView) {
                super(itemView);
                txt_title = (TextView) itemView.findViewById(R.id.txt_title);
                txt_question = (iView) itemView.findViewById(R.id.txt_question);
                triangleView = (com.jingan.nursinghome.regulatory.view.triangleView) itemView.findViewById(R.id.triangleView);
                ProportionUtils.getInstance().adapterTextView(txt_title);
                spanerScore = (SpannerView) itemView.findViewById(R.id.spanerScore);
                ProportionUtils.getInstance().adapterView(spanerScore);
                rectifi = (RectificationView) itemView.findViewById(R.id.rectifi);
                ling = itemView.findViewById(R.id.ling);
                ProportionUtils.getInstance().adapterView(ling);
            }
        }


        @Override
        public ZhangaiXiangDialog.MicrometerTableAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_micrometertablleos_item, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。
            return new ZhangaiXiangDialog.MicrometerTableAdapter.MyViewHolder(view);
        }

        String cScore;

        @Override
        public void onBindViewHolder(final ZhangaiXiangDialog.MicrometerTableAdapter.MyViewHolder holder, final int position) {
//            if (myTopics.get(position).getIndex() == null) {
//                return;
//            }
            ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(holder.txt_question.getLayoutParams());
            layoutParams.leftToLeft = holder.itemView.findViewById(R.id.left).getId();
//            if (myTopics.get(position).getIndex().trim().equals("0")) {
//                holder.txt_title.setText(myTopics.get(position).getTitle());//标题
//                holder.triangleView.setInfo(myTopics.get(position).getTitleIndex());//标题下标
//                layoutParams.topToTop = holder.itemView.findViewById(R.id.top).getId();
//                holder.ling.setVisibility(View.GONE);
//            } else {
            layoutParams.topToTop = holder.itemView.findViewById(R.id.top3).getId();
            holder.txt_title.setText(null);
            holder.triangleView.setInfo(null);
            holder.ling.setVisibility(View.VISIBLE);
//            }
            holder.txt_question.setLayoutParams(layoutParams);
//            String i = myTopics.get(position).getCount();//题号
            String i = (position + 1) + "";
            if (i.length() == 1) {
                i = "0" + i;
            }
            holder.txt_question.setInfo(i, myTopics.get(position).getQuestion());//题目
            //holder.spanerScore.setInfo("  "+myTopics.get(position).getMaxScore() + "分");//分数
            cScore = myTopics.get(position).getCscore();
            if (cScore == null || cScore.trim().equals("")) {
                cScore = myTopics.get(position).getMaxScore();
                holder.spanerScore.setIsscore(false);
            } else {
                holder.spanerScore.setIsscore(true);
            }
            holder.spanerScore.setInfoAndColor("  " + cScore + "分", Color.parseColor("#444444"));
            holder.spanerScore.setTextSize(ProportionUtils.getInstance().adapterInt(32));
            holder.spanerScore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ScoreDialog.getInstance(baseActivity, Integer.valueOf(cScore), Integer.valueOf(myTopics.get(position).getMaxScore()), new BaseListenner<Integer>() {
                        @Override
                        public void onResult(Integer result) {
                            holder.spanerScore.setIsscore(true);
                            holder.spanerScore.setInfoAndColor("  " + result + "分", Color.parseColor("#444444"));
                            myTopics.get(position).setCscore(result + "");

                            String c = myTopics.get(position).getCscore();
                            String m = myTopics.get(position).getMaxScore();
                            //Log.e("test","当前分数:\t"+c+"\t最大分数:\t"+m);
                            if ((c == null || c.trim().equals("")) || !c.trim().equals(m.trim())) {
                                //非满分状态。弹出是否整改的询问窗口。
                                AlertDialog.getInstance(baseActivity, false, true).setMession("是否要整改？").setPositive("确认", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        AlertDialog.dismisss();
                                        ZhengaiDialog.getInstance(baseActivity, Integer.valueOf(myTopics.get(position).getZhengai()), new BaseListenner<Integer>() {
                                            @Override
                                            public void onResult(Integer result) {
                                                myTopics.get(position).setZhengai(result + "");
                                                if (myTopics.get(position).getZhengai().equals("0")) {
                                                    holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
                                                }
                                                if (myTopics.get(position).getZhengai().equals("1")) {
                                                    holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
                                                }
                                                if (myTopics.get(position).getZhengai().equals("2")) {
                                                    holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
                                                }
                                            }
                                        });
                                    }
                                });
                            }
                        }
                    });
                }
            });

            if (myTopics.get(position).getZhengai().equals("0")) {
                holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
            }
            if (myTopics.get(position).getZhengai().equals("1")) {
                holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
            }
            if (myTopics.get(position).getZhengai().equals("2")) {
                holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
            }
            holder.rectifi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ZhengaiDialog.getInstance(baseActivity, Integer.valueOf(myTopics.get(position).getZhengai()), new BaseListenner<Integer>() {
                        @Override
                        public void onResult(Integer result) {
                            myTopics.get(position).setZhengai(result + "");
                            if (myTopics.get(position).getZhengai().equals("0")) {
                                holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
                            }
                            if (myTopics.get(position).getZhengai().equals("1")) {
                                holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
                            }
                            if (myTopics.get(position).getZhengai().equals("2")) {
                                holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
                            }
                        }
                    });
                }
            });

            //高度調整
            holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().height = ProportionUtils.getInstance().adapterInt(330) + (holder.txt_question.LineCount - 1) * ProportionUtils.getInstance().adapterInt(40);
            holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().width = (int) ProportionUtils.getInstance().realWidthPixels;
            holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).requestLayout();

            //Log.e("test", "题号:\t" + i + "\t行数:\t" + holder.txt_question.LineCount);
        }

        @Override
        public int getItemCount() {
            return myTopics.size();
        }
    }

    //适配器结束

    //适配器二

    public class MicrometerTableAdapter3 extends RecyclerView.Adapter<ZhangaiXiangDialog.MicrometerTableAdapter3.MyViewHolder> {
        BaseActivity baseActivity;

        public MicrometerTableAdapter3(BaseActivity baseActivity) {
            this.baseActivity = baseActivity;
        }

        //ViewHolder和View绑定在一起，构造函数一般都是初始化控件
        public class MyViewHolder extends RecyclerView.ViewHolder {
            iView txt_question;
            RectificationView rectifi;//整改
            EditText edit_qt;

            int position = 0;

            public MyViewHolder(View itemView) {
                super(itemView);
                if (myTopics.size() == 0) {
                    return;
                }
                txt_question = (iView) itemView.findViewById(R.id.txt_question);
                txt_question.setmWidth(ProportionUtils.getInstance().adapterInt(530));
                rectifi = (RectificationView) itemView.findViewById(R.id.rectifi);
                rectifi.setWidhtAndHeiht(ProportionUtils.getInstance().adapterInt(118), ProportionUtils.getInstance().adapterInt(66));
                edit_qt = (EditText) itemView.findViewById(R.id.edit_qt2);
                ProportionUtils.getInstance().adapterTextView(edit_qt);
            }
        }


        @Override
        public ZhangaiXiangDialog.MicrometerTableAdapter3.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (myTopics.size() == 0) {
                return new ZhangaiXiangDialog.MicrometerTableAdapter3.MyViewHolder(new KongView(parent.getContext()));
            }
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_micrometertablleos_item3, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。
            return new ZhangaiXiangDialog.MicrometerTableAdapter3.MyViewHolder(view);
        }


        @Override
        public void onBindViewHolder(final ZhangaiXiangDialog.MicrometerTableAdapter3.MyViewHolder holder, final int position) {
            if (myTopics.size() <= 0) {
                return;
            }
            String i = myTopics.get(position).getCount();//题号
            //Log.e("test","题号:\t"+i);
            holder.txt_question.setInfo(i, myTopics.get(position).getQuestion());//题目
            if (myTopics.get(position).getZhengai().equals("0")) {
                holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
            }
            if (myTopics.get(position).getZhengai().equals("1")) {
                holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
            }
            if (myTopics.get(position).getZhengai().equals("2")) {
                holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
            }


            //防止Eidtor在适配器中错乱。在holder里自主添加postion属性判断。
            holder.position = position;
            holder.edit_qt.setText(myTopics.get(position).getRemark());
            holder.edit_qt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    //为了防止eidtor适配器错乱。必须在holder里面添加一个position属性进行标示。防止错乱。以下判断亲测OK
                    if (holder.edit_qt.isFocused() && position == holder.position) {
                        myTopics.get(position).setRemark(s.toString());//s.toString()就是文本
                    }
                }
            });

            holder.rectifi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ZhengaiDialog.getInstance(baseActivity, Integer.valueOf(myTopics.get(position).getZhengai()), new BaseListenner<Integer>() {
                        @Override
                        public void onResult(Integer result) {
                            myTopics.get(position).setZhengai(result + "");
                            if (myTopics.get(position).getZhengai().equals("0")) {
                                holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
                            }
                            if (myTopics.get(position).getZhengai().equals("1")) {
                                holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
                            }
                            if (myTopics.get(position).getZhengai().equals("2")) {
                                holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
                            }
                        }
                    });
                }
            });

            //高度調整
            holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().height = ProportionUtils.getInstance().adapterInt(439) + (holder.txt_question.LineCount - 1) * ProportionUtils.getInstance().adapterInt(40);
            holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().width = (int) ProportionUtils.getInstance().realWidthPixels;
            holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).requestLayout();

            //Log.e("test", "题号:\t" + i + "\t行数:\t" + holder.txt_question.LineCount);
        }

        @Override
        public int getItemCount() {
            int count = myTopics.size();
            if (count == 0) {
                return 1;
            }
            return myTopics.size();
        }
    }

    //适配器二结束
}
